package com.kldy8.kldyw.find.project;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kldy8.kldyw.MyWebActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.find.project.bean.FilmProject;
import com.kldy8.kldyw.web.AppWebUrl;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by Administrator on 2016/8/31.
 */
public class FilmProjectAdapter extends BaseAdapter {
    private List<FilmProject> listItems;
    private LayoutInflater listContainer;           //视图容器
    ImageLoader loader;

    private Context mContext;

    // Constructor
    public FilmProjectAdapter(Context c) {
        mContext = c;
        listContainer = LayoutInflater.from(mContext);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
                .build();
        loader = ImageLoader.getInstance();
        loader.init(config);
    }

    public FilmProjectAdapter(Context c, List<FilmProject> scripts) {
        mContext = c;
        listItems = scripts;
        listContainer = LayoutInflater.from(mContext);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
                .build();
        loader = ImageLoader.getInstance();
        loader.init(config);
    }

    //记录商品选中状态
    public final class ListItemView {                //自定义控件集合
        public ImageView image;
        public TextView name;
        public ProgressBar progressBar;
        public TextView progress_text;
        public TextView supportingText;
        public TextView remaining_days;
        public TextView price;
    }

    public void replaceOrAdd(boolean isInit, List<FilmProject> listItems) {
        if (isInit) {
            this.listItems = listItems;
        } else {
            if (this.listItems != null) {
                if (listItems != null) {
                    this.listItems.addAll(listItems);
                }
            } else {
                this.listItems = listItems;
            }
        }
    }

    public int getCount() {
        // TODO Auto-generated method stub
        if (null == listItems) {
            return 1;
        } else {
            return listItems.size();
        }
    }

    @Override
    public Object getItem(int i) {
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) {

        // TODO Auto-generated method stub
        final int selectID = position;
        //自定义视图
        ListItemView listItemView = null;

            if (convertView == null)
            {
                listItemView = new ListItemView();
                //获取list_item布局文件的视图
                convertView = listContainer.inflate(R.layout.film_project_item, null);
                //获取控件对象
                listItemView.image = (ImageView) convertView.findViewById(R.id.image);
                listItemView.name = (TextView) convertView.findViewById(R.id.name);
                listItemView.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
                listItemView.progress_text = (TextView) convertView.findViewById(R.id.progress_text);
                listItemView.supportingText = (TextView) convertView.findViewById(R.id.supportingText);
                listItemView.remaining_days = (TextView) convertView.findViewById(R.id.remaining_days);
                listItemView.price = (TextView) convertView.findViewById(R.id.price);
                //设置控件集到convertView
                convertView.setTag(listItemView);
            }
            else
            {
                listItemView = (ListItemView)convertView.getTag();
            }

             Log.e("listItemView:",""+listItems.size());

            FilmProject filmProject = listItems.get(position);
            String headUrl = "" + filmProject.image;
            loader.displayImage(headUrl, listItemView.image);
            listItemView.name.setText(filmProject.name);
            listItemView.progress_text.setText(filmProject.percent);
            listItemView.supportingText.setText(filmProject.price);
            listItemView.remaining_days.setText(filmProject.remaining_days);
            listItemView.price.setText(filmProject.current_capital);

            convertView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    FilmProject filmProject = listItems.get(position);
                    String resultUrl = AppWebUrl.createUrl(filmProject.url);
                    intent.putExtra("url",resultUrl);
                    intent.setClass(mContext, MyWebActivity.class);
                    ((Activity)mContext).startActivity(intent);
                    ((Activity)mContext).overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                }
            });

            return convertView;
        }
}
